<?php
    
$sql_config=include "server_config/config.php";
$dbobj = new mysqli($sql_config['db_host'],$sql_config['db_user'],$sql_config['db_pw'],$sql_config['db_name'],$sql_config['db_port']);
if(!$dbobj->connect_error) echo"数据库连接成功！<br>";


$username = $_POST['username'];
$old_skin=$_POST['old_skin'];
$skins_type=$_POST['type'];
$change_code=$_POST['change_code'];


$skins_type=intval($skins_type);
echo "ttt";
echo $skins_type;
echo "ttt";
$stmt="";
switch($skins_type)
{
    case 1:
    {
        $stmt = $dbobj->prepare("UPDATE mc_skin_con SET skins_default = NULL WHERE username = ?");
        break;
    }
    case 2:
        {
            $stmt = $dbobj->prepare("UPDATE mc_skin_con SET skins_slim = NULL WHERE username = ?");
            break;
        }
    case 3:
        {
            $stmt = $dbobj->prepare("UPDATE mc_skin_con SET skins_cape = NULL WHERE username = ?");
            break;
        }
    case 4:
        {
            $stmt = $dbobj->prepare("UPDATE mc_skin_con SET skins_elytra = NULL WHERE username = ?");
            break;
        }
}


if ($stmt === false) {
    echo "准备语句失败: " . $dbobj->error;
}
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->close();


$delete_path="skin/". $old_skin .".png";
if ($old_skin!=null && file_exists($delete_path)) 
{
    

    if (unlink($delete_path)) 
    {
        echo "已成功删除文件: " . htmlspecialchars($delete_path);
    } 
    else 
    {
        echo "删除文件失败。";
    }
} 
else 
{
    echo "文件不存在: " . htmlspecialchars($delete_path);
}


    // 关闭数据库连接
$dbobj->close();
header("Location:change_page.php?username={$username}&change_code={$change_code}");

?>